<?php
session_start();

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $guess = $_POST['guess'];
    $_SESSION['attempts']++;
    list($guessedEyes, $guessedFace) = explode(" ", $guess);

    $correctEyes = $_SESSION['criminal']['eyes'];
    $correctFace = $_SESSION['criminal']['face'];

    if ($guessedEyes === $correctEyes && $guessedFace === $correctFace) {
        $result = [
            'correct' => true,
            'message' => "猜对啦！你一共用了 {$_SESSION['attempts']} 次尝试。游戏重新开始。",
        ];
        // 重置游戏
        $criminals = [
            ["hair" => "长发", "eyes" => "蓝色", "face" => "圆脸"],
            ["hair" => "短发", "eyes" => "棕色", "face" => "方脸"],
            ["hair" => "卷发", "eyes" => "黑色", "face" => "瓜子脸"]
        ];
        $_SESSION['criminal'] = $criminals[array_rand($criminals)];
        $_SESSION['attempts'] = 0;
    } else {
        $result = [
            'correct' => false,
            'message' => "猜错了，请继续尝试。",
        ];
    }

    header('Content-Type: application/json');
    echo json_encode($result);
}
?>